home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest1_0.lha / Tests / barrier / barrier.C next >
C/C++ Source or Header  |  1991-12-11  |  3KB  |  106 lines

  1. #include "presto.h"
  2.  
  3. #include "barrier.h"
  4.  
  5. #define debugprint
  6.  
  7. MasterSlaveBarrier::MasterSlaveBarrier (int numslaves)
  8. {
  9. #ifdef debugprint
  10.   cout << "MasterSlaveBarrier: numslaves = " << numslaves << "\n";
  11. #endif
  12.   b_monitor = new Monitor ("MonitorMasterSlaveBarrier");
  13.   master_goahead = new Condition (b_monitor,"MasterSlaveBarrierCondition");
  14.   nslaves = numslaves;
  15.   arrivals = 0;
  16.   current_lock = 0;
  17.   l[0] = new Lock ("#0MasterSlaveBarrierLock");
  18.   l[1] = new Lock ("#1MasterSlaveBarrierLock");
  19.   l[0]->lock ();
  20. }
  21.  
  22. void
  23. MasterSlaveBarrier::SlaveArrive ()
  24. {
  25.   Lock *barrier;
  26. #ifdef debugprint
  27.   cout << "SlaveArrive at " << (int) this 
  28.        << ": current_lock = " << current_lock << "\n";
  29. #endif
  30.   {
  31.     MONITOR ENTRY(b_monitor);
  32.     arrivals++;
  33.     if (arrivals == nslaves+1) {  /* all slaves plus master have arrived */
  34. #ifdef debugprint
  35.     cout << "SlaveArrive at " << (int) this 
  36.          << ": signaling master via " << (int) master_goahead << "\n";
  37. #endif
  38.       master_goahead->signal();   /* let master continue (when we're done */
  39. #ifdef debugprint
  40.     cout << "SlaveArrive at " << (int) this 
  41.          << ": done signaling master \n";
  42. #endif
  43.     }
  44.     barrier = l[current_lock];    /* remember which lock we're all using */
  45.   }
  46. #ifdef debugprint
  47.   cout << "SlaveArrive at " << (int) this 
  48.        << ": locking barrier " << (int) barrier << "\n";
  49. #endif
  50.   barrier->lock();     /* and queue up waiting for everybody to arrive */
  51. #ifdef debugprint
  52.   cout << "SlaveArrive at " << (int) this 
  53.        << ": unlocking barrier " << (int) barrier << "\n";
  54. #endif
  55.   barrier->unlock();   /* then one by one release the whole bunck */
  56. #ifdef debugprint
  57.   cout << "SlaveArrive at " << (int) this 
  58.        << ": unlocked barrier " << (int) barrier << "\n";
  59. #endif
  60. }
  61.  
  62. void
  63. MasterSlaveBarrier::MasterArrive ()
  64. {
  65.   MONITOR ENTRY(b_monitor);
  66. #ifdef debugprint
  67.   cout << "MasterArrive: current_lock = " << current_lock << "\n";
  68. #endif
  69.   arrivals++;
  70.   if (arrivals < nslaves+1) {  /* not all slaves have arrived yet */
  71. #ifdef debugprint
  72.     cout << "MasterArrive at " << (int) this << " Waiting\n" ;
  73. #endif
  74.     master_goahead->wait();    /* so wait for them */
  75.                                /* (implicitly exiting b_monitor) */
  76.   }
  77. }
  78.  
  79. void
  80. MasterSlaveBarrier::LetSlavesGo ()
  81.   // entered only with all slaves blocked in SlaveArrive
  82. {
  83.   MONITOR ENTRY(b_monitor);
  84. #ifdef debugprint
  85.   cout << "LetSlavesGo at " << (int) this
  86.        << ": current_lock = " << current_lock << "\n";
  87. #endif
  88.   arrivals = 0;
  89.   current_lock = 1 - current_lock;   // switch locks for next barrier
  90. #ifdef debugprint
  91.   cout << "LetSlavesGo at " << (int) this
  92.        << " locking " << (int) l[current_lock] << "\n";
  93. #endif
  94.   l[current_lock]->lock ();          // make 'em stop at next barrier
  95. #ifdef debugprint
  96.   cout << "LetSlavesGo at " << (int) this
  97.        << " unlocking " << (int) l[1-current_lock] << "\n";
  98. #endif
  99.   l[1-current_lock]->unlock ();      // and let 'em go!
  100. #ifdef debugprint
  101.   cout << "LetSlavesGo at " << (int) this 
  102.        << ": unlocked " << (int) l[1-current_lock] << "\n";
  103. #endif
  104. }
  105.  
  106.